import { createStore, combineReducers, applyMiddleware } from "redux";
import carReduce from "./car/carReduce";
import { useSelector, TypedUseSelectorHook } from "react-redux";
import logger from "redux-logger";
import thunk from "redux-thunk";
const rootReducer = combineReducers({
  car: carReduce,
});

const store = createStore(rootReducer, applyMiddleware(logger, thunk));

export type RootState = ReturnType<typeof store.getState>;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

store.subscribe(() => {
  const state = store.getState();
  localStorage.setItem("car", JSON.stringify(state.car.car));
});

export default store;
